package service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import service.edu.client.VodClient;
import service.edu.controller.VideoController;
import service.edu.entity.*;
import service.edu.entity.vo.CourseInfo;
import service.edu.entity.vo.CoursePublish;
import service.edu.entity.vo.IndexCourseAll;
import service.edu.entity.vo.IndexCourseVo;
import service.edu.mapper.CourseMapper;
import service.edu.service.ChapterService;
import service.edu.service.CourseDescriptionService;
import service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import service.edu.service.VideoService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author diao
 * @since 2020-08-05
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private ChapterService chapterService;
    @Autowired
    private VodClient vodClient;
    @Autowired
    private CourseService courseService;

    //添加课程基本信息的方法
    @Override
    public String saveCourseInfo(CourseInfo courseInfo) {
        //1.向课程表添加课程基本信息
        Course course = new Course();
        BeanUtils.copyProperties(courseInfo,course);
        baseMapper.insert(course);
        //2.向课程描述表添加简介信息
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setDescription(courseInfo.getDescription());
        //为了使课程表和课程描述表一一对应,故描述id就是课程id
        String id = course.getId();
        courseDescription.setId(id);
        courseDescriptionService.save(courseDescription);
        return id;
    }

    //根据id查询课程基本信息
    @Override
    public CourseInfo getCourseInfo(String courseId) {
        Course course = baseMapper.selectById(courseId);
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(course,courseInfo);
        //还需要查询课程简介信息
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        if (courseDescription!=null)
        {courseInfo.setDescription(courseDescription.getDescription());}
        return courseInfo;
    }

    //根据id修改课程信息
    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseInfo,course);
        baseMapper.updateById(course);
        //同时需要修改课程描述表
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(courseInfo.getId());
        courseDescription.setDescription(courseInfo.getDescription());
        courseDescriptionService.updateById(courseDescription);
    }

    //根据课程id发布课程
    @Override
    public CoursePublish getCoursePublishInfo(String courseId) {
        return baseMapper.getCoursePublishInfo(courseId);
    }

    //根据课程id删除课程以及课程里的章节小节等内容
    @Override
    public void removeCourse(String courseId) {
        //根据课程id查询所有的小节以便获得所有视频id
        QueryWrapper<Video> videoSourceWrapper = new QueryWrapper<>();
        videoSourceWrapper.eq("course_id", courseId);
        //videoSourceWrapper.select("video_source_id");只查询出该字段的video对象
        List<Video> videoList = videoService.list(videoSourceWrapper);
        List<String> videoIdList = new ArrayList<>();
        for (int i = 0; i <videoList.size() ; i++) {
            Video video = videoList.get(i);
            String videoSourceId = video.getVideoSourceId();
            //小节的视频id不为空才能放入视频id列表中
            if (!StringUtils.isEmpty(videoSourceId))
            {videoIdList.add(videoSourceId);}
        }
        //根据视频id列表删除多个视频
        if (videoIdList.size()>0)
        {vodClient.deleteVideoBatch(videoIdList);}
        //根据课程id删除小节
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoService.remove(videoQueryWrapper);
        //根据课程id删除章节
        QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterService.remove(chapterQueryWrapper);
        //根据课程id删除课程描述
        QueryWrapper<CourseDescription> courseDescriptionQueryWrapper = new QueryWrapper<>();
        courseDescriptionQueryWrapper.eq("id", courseId);//因为课程描述id等于课程id
        courseDescriptionService.remove(courseDescriptionQueryWrapper);
        //删除课程本身
        baseMapper.deleteById(courseId);
     }

    //首页课程分页条件查询
    @Override
    public HashMap<String, Object> getIndexCourseListByCondition(Page<Course> page, IndexCourseVo indexCourseVo) {
        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        //获得条件值
        String gmtCreateSort = indexCourseVo.getGmtCreateSort();
        String priceSort = indexCourseVo.getPriceSort();
        String viewCountSort = indexCourseVo.getViewCountSort();
        String subjectId = indexCourseVo.getSubjectId();
        String subjectParentId = indexCourseVo.getSubjectParentId();
        //判断条件值是否为空,不为空则拼接判断条件
        if (!StringUtils.isEmpty(subjectParentId)){//一级分类
            courseWrapper.eq("subject_parent_id", subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)){//二级分类
            courseWrapper.eq("subject_id", subjectId);
        }
        if (!StringUtils.isEmpty(gmtCreateSort)){//时间排序
            courseWrapper.orderByAsc("gmt_create");
        }
        if (!StringUtils.isEmpty(priceSort)){//价格排序
            courseWrapper.orderByAsc("price");
        }
        if (!StringUtils.isEmpty(viewCountSort)){//销量排序
            courseWrapper.orderByAsc("view_count");
        }

        //进行分页条件查询
        baseMapper.selectPage(page, courseWrapper);
        //获取分页结果
        long current = page.getCurrent();
        long pages = page.getPages();
        List<Course> courseList = page.getRecords();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();
        //将分页结果添加至map中,其中包括查询出的课程列表
        HashMap<String, Object> map = new HashMap<>();
        map.put("courseList", courseList);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    //根据课程id通过sql语句进行复杂查询
    @Override
    public IndexCourseAll getIndexCourseInfoByCourseId(String courseId) {
        IndexCourseAll indexCourseAll= baseMapper.getIndexCourseInfo(courseId);
        return indexCourseAll;
    }

    //根据课程相似名查询相关课程详情
    @Override
    public String queryCourseByLikeName(String courseName) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.like("title", courseName);
        Course course = baseMapper.selectOne(wrapper);
        String courseId = course.getId();
        return courseId;
    }
}
